<?php
App::uses('AppModel', 'Model');
/**
 * Aluno Model
 *
 */
class Aluno extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_alunos';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'nome';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_alunos' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'nome' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Nome não pode ser vazio',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				'message' => 'E-mail inválido',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	public $hasMany = array(
		'Resenhas' => array(			
            'className'     => 'Resenha',
            'foreignKey'    => 'alunos_id',
		),
		'Historico' => array(
			'className'     => 'HistoricoEmprestimo',
            'foreignKey'    => 'alunos_id',
		),
		'Emprestimos' => array(
			'className'     => 'Emprestimo',
            'foreignKey'    => 'alunos_id',
		),
	);
	
	function beforeSave($options = array()) {
		if(isset($this->data['Aluno']['senha']))
        	 $this->data['Aluno']['senha'] = AuthComponent::password($this->data['Aluno']['senha']);
        return true;
    }
    
	public function emprestimo($aluno){
		$this->read(null, $aluno['id_alunos']);
		$this->save(array('emprestimos'=>$aluno['emprestimos']+1));
	}
}
